package com.example.imtoken.modules.main.wallet.manage.home

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.databinding.ActivityHomeWalletManageBinding
import com.example.imtoken.modules.main.wallet.search.SearchWalletActivity

class HomeWalletManageActivity :
    BaseActivity<HomeWalletManageViewModel, ActivityHomeWalletManageBinding>(
        HomeWalletManageViewModel::class.java
    ) {

    private lateinit var adapter: HomeWalletManageAdapter

    override fun initLayoutId(): Int {
        return R.layout.activity_home_wallet_manage
    }

    override fun init() {
        initHeadView()
        initAdapter()

        initObserve()

        viewModel.getWalletAddress()
    }

    private fun initObserve() {
        viewModel.getDataLiveData().observe(this) {
            adapter.setDiffNewData(it)
        }
    }

    private fun initAdapter() {
        adapter = HomeWalletManageAdapter(R.layout.item_home_wallet_manage)
        adapter.setDiffCallback(HomeWalletManageDiffCallback())
        dataBinding.rvHomeWallet.layoutManager = LinearLayoutManager(this)
        dataBinding.rvHomeWallet.adapter = adapter
        adapter.setOnItemChildClickListener { _, _, position ->
            viewModel.removeWalletAddress(position)
        }
    }

    private fun initHeadView() {
        dataBinding.headView.ivHeadBack.setOnClickListener {
            this.finish()
        }
        dataBinding.headView.ivHeadFunction.setOnClickListener {
            ActivityUtils.startActivity(SearchWalletActivity::class.java)
        }
        dataBinding.headView.tvHeadTitles.text = getString(R.string.homeAssetManagement)
        dataBinding.headView.ivHeadFunction.visibility = View.VISIBLE
        dataBinding.headView.ivHeadFunction.setImageResource(R.drawable.ic_baseline_search_black_24)
    }
}